package com.appspot.bibtable.model.format;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.appspot.bibtable.model.Reference;

public abstract class DownloadFormatter implements Formatter<Reference>
{
	private static final Map<String, DownloadFormatter> instanceCollection;
	
	static
	{
		Map<String, DownloadFormatter> map = new HashMap<String, DownloadFormatter>();
		map.put("plain", new PlainDownloadFormatter());
		instanceCollection = Collections.unmodifiableMap(map);
	}
	
	private PrintWriter writer;

	protected DownloadFormatter()
	{
	}

	public static DownloadFormatter getInstance(String format)
	{
		if (!instanceCollection.containsKey(format))
		{
			throw new IllegalArgumentException("Unknown format (" + format + ").");
		}
		return instanceCollection.get(format);
	}

	public static Set<String> getAvailableFormats()
	{
		return instanceCollection.keySet();
	}
	
	public PrintWriter getWriter()
	{
		return writer;
	}
	
	public void setWriter(PrintWriter writer)
	{
		this.writer = writer;
	}
	
	public String getContentType()
	{
		return "application/octet-stream";
	}
	
	public String getExtension()
	{
		return "";
	}
	
	public abstract void startDownload();
	public abstract void startItem(Reference reference, int index);
	public abstract void endItem(Reference reference, int index);
	public abstract void endDownload();
}
